package com.zlx.sharelive.activity;

import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zlx.module_base.base_ac.BaseAc;
import com.zlx.module_base.base_api.res_data.BaseInfoBean;
import com.zlx.module_base.base_api.res_data.UserLevelVoBean;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.R;
import com.zlx.sharelive.adapter.CommonRecyclerViewAdapter;
import com.zlx.sharelive.adapter.CommonRecyclerViewHolder;
import com.zlx.sharelive.utils.GlideImgManager;
import com.zlx.sharelive.utils.StringUt;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 我的等级
 */
public class GradeAc extends BaseAc {

    @BindView(R.id.iv_head)
    ImageView iv_head;
    @BindView(R.id.tv_nickname)
    TextView tv_nickname;
    @BindView(R.id.sb_view)
    SeekBar sb_view;
    @BindView(R.id.tv_score)
    TextView tv_score;
    @BindView(R.id.tv_score_max)
    TextView tv_score_max;
    @BindView(R.id.tv_lovel_text)
    TextView tv_lovel_text;
    @BindView(R.id.rv_lovel)
    RecyclerView rv_lovel;
    @BindView(R.id.tv_level)
    TextView tv_level;

    private BaseInfoBean mBaseInfoBean;
    private List<UserLevelVoBean.InfoDTO.ListDTO> mList = new ArrayList<>();
    private CommonRecyclerViewAdapter<UserLevelVoBean.InfoDTO.ListDTO> mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.ac_grade;
    }

    @Override
    public void initViews() {
        super.initViews();

        mBaseInfoBean = (BaseInfoBean) getIntent().getSerializableExtra("info");

        initRecyclerView();

        sb_view.setClickable(false);
        sb_view.setEnabled(false);
        sb_view.setSelected(false);
        sb_view.setFocusable(false);

        setInfoData();

        getUserLevelVo();

    }

    private void initRecyclerView() {
        rv_lovel.setLayoutManager(new GridLayoutManager(this, 3));

        mAdapter = new CommonRecyclerViewAdapter<UserLevelVoBean.InfoDTO.ListDTO>(this, mList) {
            @Override
            public void convert(CommonRecyclerViewHolder holder, UserLevelVoBean.InfoDTO.ListDTO entity, int position) {
                GlideImgManager.glideLoader(GradeAc.this, entity.getImg(), holder.getView(R.id.iv_lovel), 0);
                holder.setText(R.id.tv_lovel, entity.getLevelName());
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_lovel;
            }
        };

        rv_lovel.setAdapter(mAdapter);
    }

    private void getUserLevelVo() {
        ApiUtil.getUserApi().getUserLevelVo(SPUtil.getToken(this)).observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<UserLevelVoBean>>() {
            @Override
            public void onSuccess(ApiResponse<UserLevelVoBean> data) {
                mList.clear();
                if (null != data) {
                    if (null != data.getData()) {
                        if (null != data.getData().getInfo()) {
                            if (data.getData().getInfo().size() > 0) {
                                setLovelData(data.getData().getInfo().get(0));
                                if (null != data.getData().getInfo().get(0).getList()) {
                                    mList.addAll(data.getData().getInfo().get(0).getList());
                                }
                            }
                        }
                    }
                }
                mAdapter.notifyDataSetChanged();
            }
        }));
    }

    private void setLovelData(UserLevelVoBean.InfoDTO data) {
        try {
            tv_score.setText(data.getScore());
            tv_score_max.setText("LV" + data.getLevel());
            if (!StringUt.isEmpty(data.getLevel())) {
                int leve = (Integer.valueOf(data.getLevel()) - 1);
                tv_level.setText("当前等级LV" + leve);
            }
            if (!StringUt.isEmpty(data.getScore())) {
                sb_view.setProgress(Integer.valueOf(data.getScore()));//设置起始时的进度
            }
            tv_lovel_text.setText(Html.fromHtml(data.getText(), null, null));
        } catch (Exception e) {
            Log.d("ret", "joker 等级显示数据异常了");
        }
    }


    private void setInfoData() {
        if (null != mBaseInfoBean) {
            if (null != mBaseInfoBean.getInfo()) {
                if (mBaseInfoBean.getInfo().size() > 0) {
                    if (!StringUt.isEmpty(mBaseInfoBean.getInfo().get(0).getAvatar())) {
                        GlideImgManager.glideLoader(this, mBaseInfoBean.getInfo().get(0).getAvatar(),
                                iv_head, 0);
                    }
                    tv_nickname.setText(mBaseInfoBean.getInfo().get(0).getNickname());
                }
            }
        }
    }

    @OnClick(R.id.iv_back)
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
